package cn.tecnova.cd

import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{Dataset, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.rdd.EsSpark

/**
  * description:
  * 343 8987
  **/
object TecnovaPatent {
  Logger.getLogger("org").setLevel(Level.ERROR)

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val rowDF = sqLContext.read.jdbc("jdbc:mysql://www.slave4.com:3306/tec_patent", "qichacha_patent","id",1,3438987,10, ConfigHandler.props2)

    //导入写es相关的包
    import org.elasticsearch.spark.sql._

    val result: Dataset[TecnovaPatent] = rowDF.map(row => {
      TecnovaPatent(
        row.getAs[Int]("id"),
        row.getAs[String]("KeyNo"),
        row.getAs[String]("KeyNop"),
        row.getAs[String]("IPCList"),
        row.getAs[String]("KindCode"),
        row.getAs[String]("ApplicationNumber"),
        row.getAs[String]("ApplicationDate"),
        row.getAs[String]("PublicationNumber"),
        row.getAs[String]("PublicationDate"),
        row.getAs[String]("LegalStatus"),
        row.getAs[String]("LegalStatusDesc"),
        row.getAs[String]("Title"),
        row.getAs[String]("Agency"),
        row.getAs[String]("KindCodeDesc"),
        row.getAs[String]("IPCDesc"),
        row.getAs[String]("InventorStringList"),
        row.getAs[String]("AssigneestringList")

      )
    })

//    result.saveToEs("tecnova_patent2" + "/company_patent")

    EsSpark.saveToEs(result.rdd,"tecnova_patent/company_patent",Map("es.mapping.id" -> "id"))


    sc.stop()

  }

}

case class TecnovaPatent(

                           var id:Long,
                           var keyNo:String,
                           var keyNop:String,
                           var iPCList:String,
                           var kindCode:String,
                           var applicationNumber:String,
                           var applicationDate:String,
                           var publicationNumber:String,
                           var PublicationDate:String,
                           var legalStatus:String,
                           var legalStatusDesc:String,
                           var title:String,
                           var agency:String,
                           var kindCodeDesc:String,
                           var iPCDesc:String,
                           var inventorStringList:String,
                           var assigneestringList:String

                         )
